package top.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import top.api.Constants.SystemConstants;
import top.api.common.ResponseResult;
import top.api.domain.dto.LinkAddDto;
import top.api.domain.dto.LinkUpdateDto;
import top.api.domain.dto.LinkUpdateStatusDto;
import top.api.domain.entity.Link;
import top.api.domain.vo.PageVo;
import top.api.enums.AppHttpCodeEnum;
import top.api.exception.SystemException;
import top.api.service.LinkService;
import top.api.mapper.LinkMapper;
import org.springframework.stereotype.Service;
import top.api.utils.BeanCopyUtils;
import top.api.domain.vo.LinkVo;

import java.util.List;

/**
* @author 27465
* @description 针对表【sg_link(友链)】的数据库操作Service实现
* @createDate 2023-01-29 17:21:45
*/
@Service
public class LinkServiceImpl extends ServiceImpl<LinkMapper, Link>
    implements LinkService{

    @Autowired
    private LinkMapper linkMapper;

    @Override
    public ResponseResult getAllLink() {
        // 查询状态为 0 得 友链
        LambdaQueryWrapper<Link> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Link::getStatus, SystemConstants.LINK_STATUS_NORMAL);

        List<Link> links = linkMapper.selectList(queryWrapper);

        // 拷贝Vo
        List<LinkVo> linkVos = BeanCopyUtils.copyBeanList(links, LinkVo.class);

        return ResponseResult.okResult(linkVos);
    }

    @Override
    public ResponseResult getLinkPage(Integer pageNum, Integer pageSize, String name, String status) {
        LambdaQueryWrapper<Link> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(name),Link::getName,name);
        queryWrapper.eq(StringUtils.hasText(status),Link::getStatus,status);

        // 分页查询
        Page<Link> pageInfo = new Page<>(pageNum,pageSize);
        page(pageInfo,queryWrapper);

        List<LinkVo> linkVos = BeanCopyUtils.copyBeanList(pageInfo.getRecords(), LinkVo.class);

        PageVo pageVo = new PageVo(linkVos,pageInfo.getTotal());

        return ResponseResult.okResult(pageVo);

    }

    @Override
    public ResponseResult addLink(LinkAddDto linkAddDto) {
        Link link = BeanCopyUtils.copyBean(linkAddDto, Link.class);
        check(link);

        linkMapper.insert(link);

        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getLinkInfo(Long id) {
        LambdaQueryWrapper<Link> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Link::getId,id);

        Link link = linkMapper.selectOne(queryWrapper);

        LinkVo linkVo = BeanCopyUtils.copyBean(link, LinkVo.class);

        return ResponseResult.okResult(linkVo);
    }

    @Override
    public ResponseResult updateLink(LinkUpdateDto linkUpdateDto) {
        Link link = BeanCopyUtils.copyBean(linkUpdateDto, Link.class);
        check(link);

        linkMapper.updateById(link);

        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteLink(Long id) {
        linkMapper.deleteById(id);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult updateLinkStatus(LinkUpdateStatusDto linkUpdateDto) {
        if (linkUpdateDto.getId() == null){
            throw new SystemException(AppHttpCodeEnum.CONTENT_NOT_NULL);
        }else if (linkUpdateDto.getStatus() == null){
            throw new SystemException(AppHttpCodeEnum.CONTENT_NOT_NULL);
        }
        Link link = BeanCopyUtils.copyBean(linkUpdateDto, Link.class);
        linkMapper.updateById(link);

        return ResponseResult.okResult();
    }

    private  void check(Link link){
        if (! StringUtils.hasText(link.getName())){
            throw new SystemException(AppHttpCodeEnum.CONTENT_NOT_NULL);
        }else if (! StringUtils.hasText(link.getAddress())){
            throw new SystemException(AppHttpCodeEnum.CONTENT_NOT_NULL);
        }else if (! StringUtils.hasText(link.getStatus())){
            throw new SystemException(AppHttpCodeEnum.CONTENT_NOT_NULL);
        }

        LambdaQueryWrapper<Link> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Link::getName,link.getName());

        Integer count = linkMapper.selectCount(queryWrapper);

        if (count > 0){
            throw new SystemException(AppHttpCodeEnum.LINK_EXIST);
        }
    }
}




